package com.proton.carepatchtemp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapterholder.MsgHolder;
import com.proton.carepatchtemp.database.table.Msg;
import com.proton.carepatchtemp.utils.net.MsgCenter;
import com.sinping.iosdialog.dialog.widget.ActionSheetDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Api on 2016/11/3.
 */

public class MsgAdapter extends BaseAdapter<MsgHolder> {

    //  是否批量删除
    public boolean isBulkDeletion = false;
    //  批量删除列表
    //  id列表
    public List<Long> mDeleteList = new ArrayList<>();
    //  Report对象列表
    public List<Msg> mDelMsgList = new ArrayList<>();
    public OnItemClickListener mOnItemClickListener;
    private Context mContext;
    private LayoutInflater mInflater;

    public MsgAdapter(List datas, Context context) {
        super(datas);
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public MsgHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        MsgHolder holder = new MsgHolder(mInflater.inflate(R.layout.item_msg_system, parent, false));
        setListener(parent, holder, viewType);
        return holder;
    }

    protected void setListener(final ViewGroup parent, final MsgHolder holder, int viewType) {
        holder.itemView.setOnClickListener(v -> {
            if (null != mOnItemClickListener) {
                int position = holder.getAdapterPosition();
                mOnItemClickListener.onItemClick(v, holder, datas.get(position), position);
            }
        });

        holder.itemView.setOnLongClickListener(v -> {
            if (mOnItemClickListener != null) {
                int position = holder.getAdapterPosition();
                LogUtils.e(datas.get(position) + " " + position);
                return mOnItemClickListener.onItemLongClick(v, holder, datas.get(position), position);
            }
            return false;
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(MsgHolder holder, int position) {
        try {
            final Msg mMsg = (Msg) datas.get(position);

            //  是否批量删除
            if (isBulkDeletion) {
                holder.mSwipeMenuLayout.setSwipeEnable(false);
                holder.mCbMsgSelect.setVisibility(View.VISIBLE);
                holder.mCbMsgSelect.setChecked(mMsg.isChecked());
                holder.mCbMsgSelect.setOnClickListener(v -> {
                    if (mDelMsgList.contains(mMsg)) {
                        mMsg.setChecked(false);
                        holder.mCbMsgSelect.setChecked(false);
                        mDelMsgList.remove(mMsg);
                        mDeleteList.remove(mMsg.getId() + "");
                    } else {
                        mMsg.setChecked(true);
                        holder.mCbMsgSelect.setChecked(true);
                        mDelMsgList.add(mMsg);
                        mDeleteList.add(mMsg.getId());
                    }
                    LogUtils.d(mDeleteList);
                    LogUtils.d(mDelMsgList);
                    LogUtils.e("mDeleteList = " + mDeleteList.toString().replace("[", "").replace("]", ""));
                });
            } else {
                holder.mSwipeMenuLayout.setSwipeEnable(true);
                holder.mCbMsgSelect.setVisibility(View.GONE);
            }


            holder.mTvMsgContent.setText(mMsg.getContents());
            holder.mTvMsgTime.setText(mMsg.getTime());
            holder.mTvMsgTitle.setText(mMsg.getTitle());
            holder.mIvDelete.setOnClickListener(v -> {

                final String[] stringItems = {mContext.getString(R.string.string_confirm)};
                final ActionSheetDialog dialog = new ActionSheetDialog(mContext, stringItems, holder.itemView);
                dialog.title(mContext.getString(R.string.string_delete_msg_warm));
                dialog.titleTextSize_SP(14F);
                dialog.show();
                dialog.setOnOperItemClickL((parent, view1, position1, id) -> {
                    switch (position1) {
                        case 0:
                            holder.mSwipeMenuLayout.quickClose();
                            MsgCenter.msgdelete(((Msg) datas.get(position)).getId());
                            datas.remove(position);
                            notifyDataSetChanged();
                            break;
                        default:
                            break;
                    }
//            TastyUtils.makeTextShort(stringItems[position], TastyToast.SUCCESS);
                    dialog.dismiss();
                });
            });

        } catch (Exception e) {
            LogUtils.e(e);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, RecyclerView.ViewHolder holder, Object o, int position);

        boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, Object o, int position);
    }
}
